package de.duester.vectordrawing;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.PointF;

/**
 * @author Maxim Duester
 */
public class Layer implements IPaintable {
	private String name;
	private boolean visible = true;
	private List<GraphicalObject> objects = new ArrayList<GraphicalObject>();

	/**
	 * 
	 */
	public Layer() {
		// TODO Layer()
	}

	/**
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param object
	 */
	public void addObject(GraphicalObject object) {
		if (!objects.contains(object))
			objects.add(object);
	}

	/**
	 * @param object
	 */
	public void removeObject(GraphicalObject object) {
		objects.remove(object);
	}

	/**
	 * @param position
	 * @return
	 * @throws IndexOutOfBoundsException
	 */
	public GraphicalObject getObject(int position) throws IndexOutOfBoundsException {
		return objects.get(position);
	}

	/**
	 * @param object
	 * @return
	 */
	public boolean contains(GraphicalObject object) {
		return objects.contains(object);
	}

	/**
	 * @param object
	 * @param layer
	 */
	public void moveToLayer(GraphicalObject object, Layer layer) {
		if (!objects.contains(object))
			return;
		removeObject(object);
		layer.addObject(object);
	}

	/**
	 * @return
	 */
	public int size() {
		return objects.size();
	}

	/**
	 * 
	 */
	public void clear() {
		objects.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Layer))
			return false;
		Layer l = (Layer) o;
		if (name == null || l.name == null)
			return false;
		return name.equals(l.name);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#move(android.graphics.PointF)
	 */
	public void move(PointF p) {
		for (GraphicalObject object : objects)
			object.move(p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#moveRelative(android.graphics.PointF)
	 */
	public void moveRelative(PointF p) {
		for (GraphicalObject object : objects)
			object.moveRelative(p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#rotate(int, android.graphics.PointF)
	 */
	public void rotate(int angle, PointF p) {
		for (GraphicalObject object : objects)
			object.rotate(angle, p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#rotateAndMove(int, android.graphics.PointF)
	 */
	public void rotateAndMove(int angle, PointF p) {
		for (GraphicalObject object : objects)
			object.rotateAndMove(angle, p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#scale(double, android.graphics.PointF)
	 */
	public void scale(double factor, PointF p) {
		for (GraphicalObject object : objects)
			object.scale(factor, p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IPaintable#setVisible(boolean)
	 */
	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IPaintable#isVisible()
	 */
	public boolean isVisible() {
		return visible;
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IPaintable#paint(android.graphics.Canvas)
	 */
	public void paint(Canvas canvas) {
		for (GraphicalObject object : objects)
			if (object.isVisible())
				object.paint(canvas);
	}
}
